package ua.eug.airv1.utils;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class Vector2i implements Externalizable {
	
	// placeholder
	public static final Vector2i Zero = new Vector2i(0,0);

	/** the x-component of this vector **/
	public int x;
	/** the y-component of this vector **/
	public int y;

	/** Constructs a new vector at (0,0) */
	public Vector2i () {
	}

	/** Constructs a vector with the given components
	 * @param x The x-component
	 * @param y The y-component */
	public Vector2i (int x, int y) {
		this.x = x;
		this.y = y;
	}

	/** Constructs a vector from the given vector
	 * @param v The vector */
	public Vector2i (Vector2i v) {
		set(v);
	}
	
	public Vector2i cpy () {
		return new Vector2i(this);
	}

	public Vector2i set (Vector2i v) {
		x = v.x;
		y = v.y;
		return this;
	}

	public Vector2i set(int i, int j) {
		x = i;
		y = j;
		return this;
	}

	public Vector2i sub (Vector2i v) {
		x -= v.x;
		y -= v.y;
		return this;
	}

	/** Substracts the other vector from this vector.
	 * @param x The x-component of the other vector
	 * @param y The y-component of the other vector
	 * @return This vector for chaining */
	public Vector2i sub (int x, int y) {
		this.x -= x;
		this.y -= y;
		return this;
	}

	public Vector2i add (Vector2i v) {
		x += v.x;
		y += v.y;
		return this;
	}

	/** Adds the given components to this vector
	 * @param x The x-component
	 * @param y The y-component
	 * @return This vector for chaining */
	public Vector2i add (int x, int y) {
		this.x += x;
		this.y += y;
		return this;
	}

	public String toString () {
		return "[" + x + ":" + y + "]";
	}

	// check bounds
	public Boolean bounds(int x1, int y1, int x2, int y2) {
		return (x>=x1 && x<x2 && y>=y1 && y<y2);
	}

	public Boolean eq(Vector2i v) {
		return (v.x == x && v.y == y);
	}

	@Override
	public void readExternal(ObjectInput oin) throws IOException,
			ClassNotFoundException {
		x=oin.readInt();
		y=oin.readInt();		
	}

	@Override
	public void writeExternal(ObjectOutput oout) throws IOException {
		oout.writeInt(x);
		oout.writeInt(y);
	}
	
}
